﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

//数据管理类

public class DataManager : SingletonMono<DataManager>
{
    public List<InventoryItem> inventoryInfos = new List<InventoryItem>();
    public List<SkillItem> skillInfos = new List<SkillItem>();
    public List<RuleItem> ruleInfos = new List<RuleItem>();
    public List<int> selectedSkillIds = new List<int>();//快捷栏技能ID
    public List<int> itemIds = new List<int>(); //背包里装备ID
    public List<ShortCut> shorcuts = new List<ShortCut>();
    public List<EquipPos> equipPoses = new List<EquipPos>();
    public List<CampBase> campbases = new List<CampBase>();
    public List<int> arrangeSkillIds = new List<int>();

    void Start()
    {
        
    }

    public void addItem()
    {

        itemIds.Add(1);
        itemIds.Add(2);
        itemIds.Add(3);
        itemIds.Add(4);
        selectedSkillIds.Add(1);
        selectedSkillIds.Add(2);

        //初始化装备格子
        initEquipSlot();
        //初始化阵基信息
        initCampbase();
    }

    public void readGameInfo()
    {
        GameInfo list = LoadJson.LoadJsonFromFile<GameInfo>("Gameinfo");
        inventoryInfos.Clear();
        //读取道具，装备
        foreach (var item in list.items)
        {
            inventoryInfos.Add(item);
        }
        //读取技能
        foreach (var skill in list.skills)
        {
            skillInfos.Add(skill);
        }
        ruleInfos.Clear();
        //读取法则
        foreach (var rule in list.rules)
        {
            ruleInfos.Add(rule);
        }
        

    }

    void initEquipSlot()
    {
        EquipPos pos0 = new EquipPos();
        pos0.id = 0;
        pos0.gridIndex = 0;
        equipPoses.Add(pos0);

        EquipPos pos = new EquipPos();
        pos.id = 0;
        pos.gridIndex = 1;
        equipPoses.Add(pos);

        EquipPos pos2 = new EquipPos();
        pos2.id = 0;
        pos2.gridIndex = 2;
        equipPoses.Add(pos2);

        EquipPos pos3 = new EquipPos();
        pos3.id = 0;
        pos3.gridIndex = 3;
        equipPoses.Add(pos3);
    }

    void initCampbase()
    {
        addCampItem(1, 1, 1);
        addCampItem(2, 2, 2);
        addCampItem(3, 3, 3);
        addCampItem(4, 4, 4);
    }

    /// <summary>
    /// 添加阵眼里的信息
    /// </summary>
    /// <param name="id"></param>
    /// <param name="ruleNum"></param>
    /// <param name="skillNum"></param>
    void addCampItem(int id,int ruleNum,int skillNum)
    {
        CampBase c = new CampBase();
        c.id = id;
        c.ruleNum = ruleNum;
        c.skillNum = skillNum;
        c.skillPoses = new List<int>();
        c.ruleIds = new List<int>();
     

        /*for (int i = 0; i < ruleNum; i++)
        {
            CampRulePos pos = new CampRulePos();
            pos.gridIndex = i;
            pos.id = -1;
            c.ruleIds.Add(pos);
        }*/
        campbases.Add(c);
    }

 

    /// <summary>
    /// 穿戴装备 
    /// </summary>
    /// <param name="gridIndex"></param>
    /// <param name="equipId"></param>
    public void equipItem(int gridIndex, int equipId)
    {
        EquipPos pos = equipPoses.Find(x => x.gridIndex == gridIndex);
        
        //如果格子上有装备，返回右侧装备栏
        if (pos.id != 0)
        {
            itemIds.Add(pos.id);
        }
        pos.id = equipId;
        int item = itemIds.Find(x => x == equipId);

        itemIds.Remove(item);

    }

    /// <summary>
    /// 替换阵眼技能
    /// </summary>
    /// <param name="campIndex"></param>
    /// <param name="gridIndex"></param>
    /// <param name="skillId"></param>
    public void addSkillByCamp(int campIndex, int gridIndex, int skillId )
    {
        //int addCount = 0;
        if(campbases[campIndex].skillPoses.Count< campbases[campIndex].skillNum)
        {
            campbases[campIndex].skillPoses.Add(skillId);
        } else
        {
            Debug.Log("满了");
        }
        
        /*for (int i= 0;i< campbases[campIndex].skillPoses.Count; i++)
        {
            if (addCount <=0)
            {
                if (campbases[campIndex].skillPoses[i] == 0 && campbases[campIndex].skillPoses[i] != -1)
                {
                    campbases[campIndex].skillPoses[i] = skillId;
                    addCount++;

                }
            }
            
        }*/
        string a = "";
        foreach(var b in campbases[campIndex].skillPoses)
        {
            a += (b+",");
        }
        Debug.Log(a);
        
       /* foreach(var a in campbases[campIndex].skillPoses)
        {
            if (a == 0)
            {
                campbases[campIndex].skillPoses[index] = skillId;

            }
            index++;
        }*/
    }

  

    public void addRuleByCamp(int campIndex, int gridIndex, int ruleId)
    {

        if (campbases[campIndex].ruleIds.Count < campbases[campIndex].ruleNum)
        {
            campbases[campIndex].ruleIds.Add(ruleId);
        }
        else
        {
            Debug.Log("满了");
        }
        string a = "";
        foreach (var b in campbases[campIndex].ruleIds)
        {
            a += (b + ",");
        }
        Debug.Log("ids:"+a);

        /*campbases[campIndex].ruleIds[gridIndex].id = ruleId;
        Debug.Log(campbases.ToString());*/
    }

   

    public void saveGame()
    {
        GameData data = new GameData();

        data.saveToLocal();

    }
  

    public void initGameData()
    {

        GameData data = LoadJson.LoadJsonFromFile<GameData>("Gameinfo");

        Debug.Log("创建新文件！");

        GameData obj = new GameData();
        //obj.shapeShifts = data.shapeShifts;
        obj.saveToLocal();

    }



}
